package interfaces;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import dinnerSimulation.Coord;
import dinnerSimulation.Floor;
import dinnerSimulation.Square;


/**
 * Implement the mouse event to create manually an environment configuration
 *
 */
public class ClickHandler extends MouseAdapter {
	JLabel label;
	int i,j;
	static boolean bCat=false;
	static boolean bSuit= false;
	static boolean bTable = false;
	static boolean bDinner = false;
	static boolean bRoses = false;
	static boolean bBedroomDoor = false;
	static boolean bKitchenDoor = false;
	static boolean bHallDoor = false;
	static boolean bLivingDoor = false;
	static boolean bDoor5 = false;
	static boolean bCiccio=false;

	JPopupMenu menu;
	JMenuItem roses;
	
	
	/** 
	 * @param label label that identifies a button
	 * @param i coordinate X of the mouse
	 * @param j coordinate Y of the mouse
	 */
	ClickHandler (JLabel label, int i, int j) {
		this.label = label;
		this.i = i;
		this.j = j;
		bCat=false;
		bSuit= false;
		bTable = false;
		bDinner = false;
		bRoses = false;
		bBedroomDoor = false;
		bKitchenDoor = false;
		bHallDoor = false;
		bLivingDoor = false;
		bDoor5 = false;
		bCiccio=false;
	}
	
	
	
	@Override 
	/**
	 * Choose and set the status of the square to clean/dirty/obstacle at mouse click
	 */
	public void mouseClicked (MouseEvent e) {
		super.mouseClicked(e);
		
		if (e.getButton() == MouseEvent.BUTTON1) {
			/* assert the agent is not on this square */
		//	if (GridPanel.env.agent.x == i && GridPanel.env.agent.y == j)
			//	return;
			/* make this square of the current type */
			
			//  GridPanel.env.floor.set (i, j, GridPanel.currType);
			  //label.setIcon (GridPanel.currIcon);
			//  System.out.print(i+ " "+j + "- " +GridPanel.currIcon );
			
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			/* show a popup menu to choose what to insert */
			
				menu = new JPopupMenu();
				roses = new JMenuItem("Roses");
				roses.addActionListener(new ActionListener() {
					@Override
					
					public void actionPerformed(ActionEvent arg0) {
						if(bRoses==false){
							GridPanel.currType = Square.Type.FLOWERS;
							GridPanel.currIcon = GridPanel.flowerIcon;
							GridPanel.env.floor.set (i, j, GridPanel.currType);
							label.setIcon (GridPanel.currIcon);
							bRoses =true;
						}
						/*Allow the movement of the roses during the preparation*/
						else{
							Coord oldPos= GridPanel.env.floor.search(Square.Type.FLOWERS);
							System.out.println("old pos"+ oldPos.x + " " + oldPos.y);		
							GridPanel.clean(oldPos.x, oldPos.y);
							GridPanel.currType = Square.Type.FLOWERS;
							GridPanel.currIcon = GridPanel.flowerIcon;
							GridPanel.env.floor.set (i, j, GridPanel.currType);
							label.setIcon (GridPanel.currIcon);
					
						}
					}
				});
			
			
			JMenuItem suit = new JMenuItem("Suit");
			suit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(bSuit==false){
						GridPanel.currType = Square.Type.SMOKING;
						GridPanel.currIcon = GridPanel.suitIcon;
						GridPanel.env.floor.set (i, j, GridPanel.currType);
						label.setIcon (GridPanel.currIcon);
						bSuit= true;
					}
					else{
						/*Allow the movement of the suit during the preparation*/
						Coord oldPos= GridPanel.env.floor.search(Square.Type.SMOKING);
						System.out.println("old pos"+ oldPos.x + " " + oldPos.y);		
						GridPanel.clean(oldPos.x, oldPos.y);
						//GridPanel.env.floor.set (oldPos.x, oldPos.y,  GridPanel.currType);
						GridPanel.currType = Square.Type.SMOKING;
						GridPanel.currIcon = GridPanel.suitIcon;
						GridPanel.env.floor.set (i, j, GridPanel.currType);
						label.setIcon (GridPanel.currIcon);
					
						
					}
				}
			});
			JMenuItem door = new JMenuItem("Door");
			door.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					GridPanel.currType = Square.Type.DOOR;
					GridPanel.currIcon = GridPanel.doorIcon;
					if(GridPanel.env.floor.room[i] == Floor.roomType.BEDROOM){
						bBedroomDoor= true;
					}
					if(GridPanel.env.floor.room[i] == Floor.roomType.KITCHEN){
						bKitchenDoor= true;
					}
					if(GridPanel.env.floor.room[i] == Floor.roomType.HALL){
						bHallDoor= true;
					}
					if(GridPanel.env.floor.room[i] == Floor.roomType.LIVINGROOM){
						bLivingDoor= true;
					}
					 GridPanel.env.floor.set (i, j, GridPanel.currType);
					  label.setIcon (GridPanel.currIcon);
				}
			});
		
			JMenuItem table = new JMenuItem("Table");
			table.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					GridPanel.currType = Square.Type.TABLE;
					GridPanel.currIcon = GridPanel.tableIcon;
					bTable = true;
					 GridPanel.env.floor.set (i, j, GridPanel.currType);
					  label.setIcon (GridPanel.currIcon);
				}
			});
			JMenuItem dinner = new JMenuItem("Dinner");
			dinner.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					GridPanel.currType = Square.Type.MEAL;
					GridPanel.currIcon = GridPanel.foodIcon;
					bDinner = true;
					 GridPanel.env.floor.set (i, j, GridPanel.currType);
					  label.setIcon (GridPanel.currIcon);
				}
			});
			
			JMenuItem ciccio = new JMenuItem("Ciccio");
			ciccio.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					GridPanel.currIcon = GridPanel.ciccio1Icon;
					GridPanel.env.agent.x = i;
					GridPanel.env.agent.y = j;
					bCiccio = true;
					GridPanel.currType= Square.Type.CLEAN;//cambiar
					 GridPanel.env.floor.set (i, j, GridPanel.currType);////cambiar
					  label.setIcon (GridPanel.currIcon);
				}
			});
			
			

			
			if(GridPanel.env.floor.room[i]==Floor.roomType.BEDROOM){
				
				if(bSuit==false)
					menu.add(suit);
				if(bBedroomDoor== false)
					menu.add(door);
				
				if(bCiccio==false)
					menu.add(ciccio);
				
			}
			/*The smoking can be moved during the preparation, it will appear in the menu even if already has been assigned*/
			if(GridPanel.env.floor.room[i]==Floor.roomType.BEDROOM && bSuit==true){
			
				menu.add(suit);		
			/*	GridPanel.currType = Square.Type.SMOKING;
				GridPanel.currIcon = GridPanel.suitIcon;
				GridPanel.env.floor.set (i, j, GridPanel.currType);
				label.setIcon (GridPanel.currIcon);*/
			
			}
			
			if(GridPanel.env.floor.room[i]==Floor.roomType.HALL){
				
				if(bRoses==false)
					menu.add(roses);
				if(bHallDoor == false)
					menu.add(door);
				
				if(bCiccio==false)
					menu.add(ciccio);
			}
			if(GridPanel.env.floor.room[i]==Floor.roomType.HALL && bRoses==true){
			
				
				menu.add(roses);
			
				
			}
			if(GridPanel.env.floor.room[i]==Floor.roomType.KITCHEN){
			
				if(bDinner==false)
					menu.add(dinner);
				if(bKitchenDoor == false)
					menu.add(door);
				
				if(bCiccio==false)
					menu.add(ciccio);
			}
			
			if(GridPanel.env.floor.room[i]==Floor.roomType.LIVINGROOM){
				
				if(bTable==false)
					menu.add(table);
				
				if(bLivingDoor == false)
					menu.add(door);
				if(bCiccio==false)
					menu.add(ciccio);
			}
			
			
			
			menu.show(label, e.getX(), e.getY());
		}
	}
	
	
	
	
	
		
	
	
}